from pathlib import Path
from time import time
from typing import Tuple, Union, Sequence

from .log import logger

P_TYPE = Union[str, Path, Sequence]

def get_timestamp(level: float = 1000, cut_length: int = 4) -> str:
    """
    获取时间戳字符串
    Args:
        level: 时间戳级别 默认毫秒
        cut_length: 剔除开头数字数量

    Returns:

    """
    return str(int(time() * level))[cut_length + 1:]


def get_h_m_s(t: float) -> Tuple:
    """
    秒转时分秒
    Args:
        t:

    Returns:

    """
    _second = t  # 秒
    second = _second % 60
    _minute = _second // 60  # 分
    minute = _minute % 60 if _minute >= 1 else 0
    _hour = _minute // 60  # 时
    hour = _hour if _hour >= 1 else 0
    return int(hour), int(minute), second


def x2path(p: P_TYPE, is_mk: bool = False) -> Path:
    """
    数据转 Path 对象
    Args:
        p: 地址数据
        is_mk: 是否创建文件夹

    Returns:

    """
    if isinstance(p, str):
        # logger.info(f"类型转换：{p.__class__.__name__} -> Path")
        p = Path(p)
    elif isinstance(p, Sequence):
        # logger.info(f"类型转换：{p.__class__.__name__} -> Path")
        p = Path().joinpath(*p)
    if is_mk:
        if not p.exists():
            logger.info(f"创建路径文件夹：{p.absolute()}")
            p.mkdir(parents=True, exist_ok=True)
    return p
